//
//  MZRateHud.m
//  MZSystem
//
//  Created by Beyondream on 2018/11/9.
//  Copyright © 2018 zerod. All rights reserved.
//

#import "MZRateHud.h"

@interface MZRateHud()
@property (weak, nonatomic) IBOutlet UIView *hudContentView;
@property (weak, nonatomic) IBOutlet UILabel *rateLabel;
@property (nonatomic,assign) BOOL isShow;
@end

@implementation MZRateHud

+ (instancetype) defaultRate{
    static MZRateHud * hud = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hud = [[[NSBundle mainBundle]loadNibNamed:@"MZRateHud" owner:self options:nil] firstObject];
    });
    return hud;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.hudContentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    self.backgroundColor = [UIColor clearColor];
}

- (void)showWithTitle:(NSString *)title withFrame:(CGRect)frame{
    if (![MZRateHud defaultRate].isShow) {
        UIWindow * window = [UIApplication sharedApplication].delegate.window;
        [MZRateHud defaultRate].isShow = YES;
        [[MZRateHud defaultRate] setFrame:frame];
        
        NSMutableAttributedString * tip = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"实时码率：%@",title]];
        [tip addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:234/255.0 green:71/255.0 blue:76/255.0 alpha:1] range:NSMakeRange(tip.length - title.length, title.length)];
        
        [[MZRateHud defaultRate].rateLabel setAttributedText:tip];
        [window addSubview:[MZRateHud defaultRate]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MZRateHud defaultRate].hidden = YES;
        });
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MZRateHud defaultRate].isShow = NO;
        });
    }
    
}

@end
